<?php class receive extends Model {

    function __construct()
    {
        parent::__construct();
    }
    function truncate(){
    	return $this->db->truncate('receive');
    }
    
    function sum_total_count(){
    	$this->db->select_sum('count');
    	$query = $this->db->get('receive');
    	$result = $query->result();
    	return $result;
    }
    
    
    public function join_select($cond=""){
    
    	$this->db->select('category.level as level,category.codes as codes,receive.receivetime as receivetime,receive.codes AS r_code, receive.count AS r_count, models.name AS mod_name, category.name AS category_name,category.codes AS codes, version.name AS version_name,receive.mod_id,receive.category_id,receive.version_id');
    	$this->db->from('receive');
    	$this->db->join('models','models.mod_id = receive.mod_id','left');
    	$this->db->join('category','category.category_id = receive.category_id','left');
    	$this->db->join('version','version.version_id = receive.version_id','left');
    
    	if($cond!=""){
    		$this->db->where($cond);
    	}
    	$this->db->order_by('category.codes','asc');
    	$query = $this->db->get();
    	$result = $query->result();
    //	echo $this->db->last_query();exit;
    	return $result;
    }
    
    function insert_for_excel($array){
       $this->version_id = $array['version_id'];
       $this->category_id = $array['category_id'];
       $this->mod_id = $array['mod_id'];      
       $this->codes = $array['codes']; 
       $this->receivetime = $array['receivetime'];   
       $this->current_version = $array['current_version'];
       $this->first_version = $array['first_version'];
       $this->first_version_id = $array['first_version_id'];
       $this->createtime = time();
       $re = $this->select_by_array(array('receivetime'=>$array['receivetime'],'mod_id'=>$array['mod_id'],'category_id'=>$array['category_id'],'version_id'=>$array['version_id'],'codes'=>$array['codes']));
       if(count($re)==0){
       	 $this->count = $array['count'];
         $this->db->insert('receive',$this);
         $result = $this->db->insert_id();
       }else{
       	 $count = $re[0]->count+$array['count'];
         $this->update_ar(array('count'=>$count), $re[0]->receive_id);
         $result = $re[0]->receive_id;
       }
       return $result;
    }
    
    function select_by_array($array){
       $this->db->where($array);
       $this->db->order_by('receive_id','asc');
       $query = $this->db->get('receive');       
       $result = $query->result(); 
       //echo $this->db->last_query();exit;
       return $result;
    }
    

    
    function select_s(){
    	$this->db->select('*');
        $this->db->select_sum('count');
        $this->db->group_by('version_id');
        $query = $this->db->get('receive');
        $result = $query->result(); 
        return $result;
    
    }
    function insert($arr){
       $result = $this->db->insert('receive',$arr);
        return $this->db->insert_id();
    }
    
    function getreceive_array($arr){
       $this->db->limit(1);
       $this->db->where($arr);
       $this->db->order_by("receive_id", "desc");
       $query = $this->db->get('receive');
       $result = $query->result(); 
       return $result;
    }
    
    function select($id,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where(array('receive_id'=>$id));
       }
       $this->db->order_by('receive_id desc');
       $query = $this->db->get('receive');
       $result = $query->result(); 
       return $result;
    }
    
    function select2($id,$num="",$offset=""){
    	$this->db->limit($num,$offset);
    	if($id!=""){
    		$this->db->where($id);
    	}
    	$this->db->order_by('receive_id desc');
    	$query = $this->db->get('receive');
    	$result = $query->result();
    	return $result;
    }
    
    function getcount($id,$version_id=''){
    	$where['category_id']=$id;
    	if($version_id!=""){
    	 $where['version_id']=$version_id;
    	}
        $this->db->where($where);
    	$this->db->select_sum('count');
        $query = $this->db->get('receive');
        $result = $query->result(); 
        return $result;
    }
    
    function getlast($version_id,$id){
       $this->db->where(array('category_id'=>$version_id,'version_id'=>$id));
       $this->db->select_sum('count');
       $query = $this->db->get('receive');
       $result = $query->result(); 
       return $result;
    }
    
    function update($id){
       $this->version_id = $_POST['version_id'];
       $this->category_id = $_POST['category_id'];
       $this->mod_id = $_POST['mod_id'];
       $this->count = $_POST['count'];
       $this->codes = $_POST['codes'];
       $this->contacter = $_POST['contacter'];
       
       $result = $this->db->update('receive',$this,array('receive_id'=>$id));
       return $result;
    }
    
    function update_ar($array,$id){
       $result = $this->db->update('receive',$array,array('receive_id'=>$id));
       return $result;
    }
    
    
    function del($id){
       $result = $this->db->delete('receive',array('receive_id'=>$id));
       return $result;
    }
}